﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExperimentUpon.VO;
using ExperimentUpon.Util;
using NHibernate;
using ExperimentUpon.Service.Exception;

namespace ExperimentUpon.Service.Impl
{
    public class StudentServiceImpl : IStudentService
    {
        public void batchAddOneClass(int gradeId, int specialityId, int classId, List<Student> stuList)
        { 
            ISession session = NHibernateUtils.OpenSession();
            session.BeginTransaction();
            try
            {
                Grade grade = session.Get<Grade>(gradeId);
                Speciality spe = session.Get<Speciality>(specialityId);
                Clazz clazz = session.Get<Clazz>(classId);
                foreach (Student stu in stuList)
                {
                    stu.Clazz = clazz;
                    User user = new User();
                    user.Name = stu.Name;
                    user.Account = stu.StudentId;
                    user.Password = stu.StudentId;
                    stu.User = user;
                    session.Save(stu);
                }
                session.Transaction.Commit();
            }
            catch (System.Exception e)
            {
                session.Transaction.Rollback();
                throw new BaseException(e.Message, e);
            }
            finally
            {
                session.Close();
            }
        }
    }
}
